using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;
using CARMSData.StoredProcedureCallerClasses;

namespace CARMS.Admin
{
    public partial class ConditionDetail : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDefault();
            }
        }


        private void LoadDefault()
        {
            var collection = new ActionCollection();
            collection.GetMulti(null);
 
            ConditionClass.BindActionFolder(dplPageName);
            ConditionClass.BindTableList(dplTableName);


        }

        protected void btnGenerateColumns_Click(object sender, EventArgs e)
        {
            if (dplTableName.SelectedValue.Trim().Length == 0)
            {
                Alert("please input the database table name.");
                return;
            }
 
            var dtColumns = RetrievalProcedures.GetColumnsByTable(dplTableName.SelectedValue.Trim());
 

            gridColumns.DataSource = dtColumns;
            gridColumns.DataBind();

            ////如果有选中，则显示选中状态
            var collection = new ColumnToActionCollection();
            collection.GetMulti(ColumnToActionFields.ActionId == dplPageName.SelectedValue.Trim() & ColumnToActionFields.TableName == dplTableName.SelectedValue.Trim());
            foreach (ColumnToActionEntity columnItem in collection)
            {
                foreach (GridViewRow item in gridColumns.Rows)
                {
                    var chkColumnName = item.FindControl("txtName") as CheckBox;
                    if (columnItem.ColumnName.ToUpper() == chkColumnName.Text.ToUpper())
                    {
                        chkColumnName.Checked = true;
                        var txtTitle = item.FindControl("txtTitle") as TextBox;
                        var dplColumnType = item.FindControl("dplColumnType") as DropDownList;
                        var dplPulicCategory = item.FindControl("dplPulicCategory") as DropDownList;
                        var txtSortIndex = item.FindControl("txtSortIndex") as TextBox;
                        var panelRelativeTable = item.FindControl("panelRelativeTable") as Panel;
                        var txtRelativeTableName = item.FindControl("txtRelativeTableName") as TextBox;
                        var txtRelativeColumnName = item.FindControl("txtRelativeColumnName") as TextBox;
                        var txtRelativeSearchColumnName = item.FindControl("txtRelativeSearchColumnName") as TextBox;


                        txtTitle.Text = columnItem.Title;
                        dplColumnType.SelectedValue = columnItem.ColumnType;
                        dplPulicCategory.SelectedValue = columnItem.TypeValue;
                        txtSortIndex.Text = columnItem.SortIndex;
                        txtRelativeTableName.Text = columnItem.RelativeTable;
                        txtRelativeColumnName.Text = columnItem.RelativeColumn;
                        txtRelativeSearchColumnName.Text = columnItem.RelativeSearchColumn;

                        if (columnItem.ColumnType == ColumnType.ColumnPublicItem.Value)
                        {
                            dplPulicCategory.Visible = true;
                        }
                        if (columnItem.ColumnType == ColumnType.ColumnRelativeTable.Value)
                        {
                            panelRelativeTable.Visible = true;
                        }
                    }
                }
            }
        }

         

        protected void btnSaveColumns_Click(object sender, EventArgs e)
        {
            var collection = new ColumnToActionCollection();
            collection.DeleteMulti(ColumnToActionFields.ActionId == dplPageName.SelectedValue);

            foreach (GridViewRow item in gridColumns.Rows)
            {

                var chkColumnName = item.FindControl("txtName") as CheckBox;
                if (chkColumnName.Checked)
                {
                    var txtTitle = item.FindControl("txtTitle") as TextBox;
                    var dplColumnType = item.FindControl("dplColumnType") as DropDownList;
                    var dplPulicCategory = item.FindControl("dplPulicCategory") as DropDownList;  //数据字典
                    var txtSortIndex = item.FindControl("txtSortIndex") as TextBox;
                    var txtRelativeTableName = item.FindControl("txtRelativeTableName") as TextBox;
                    var txtRelativeColumnName = item.FindControl("txtRelativeColumnName") as TextBox;
                    var txtRelativeSearchColumnName = item.FindControl("txtRelativeSearchColumnName") as TextBox;

                    var entity = new ColumnToActionEntity
                    {
                        ActionId = dplPageName.SelectedValue,
                        ColumnName = chkColumnName.Text.Trim(),
                        TableName = dplTableName.SelectedValue.Trim(),
                        Title = txtTitle.Text.Trim(),
                        ColumnType = dplColumnType.SelectedValue,
                        TypeValue = dplPulicCategory.SelectedValue,
                        SortIndex = txtSortIndex.Text.Trim(),
                        RelativeTable = txtRelativeTableName.Text.Trim(),
                        RelativeColumn = txtRelativeColumnName.Text.Trim(),
                        RelativeSearchColumn = txtRelativeSearchColumnName.Text.Trim()
                    };
                    entity.Save();
                }

            }

            base.CloseWindow(true);
        }
        protected void gridColumns_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var strTableName = dplTableName.SelectedValue.Trim();
                var strColumnName = ((DataRowView)e.Row.DataItem)["ColumnName"].ToString();

                var columnEntity = GetColumnEntityByTableColumnName(strTableName, strColumnName);
                if (columnEntity != null)
                {
                    var txtTitle = e.Row.FindControl("txtTitle") as TextBox;
                    txtTitle.Text = columnEntity.Title;

                }

                var dplColumnType = e.Row.FindControl("dplColumnType") as DropDownList;
                ControlHelper.BindColumnTypeEntity(dplColumnType);

                var dplPulicCategory = e.Row.FindControl("dplPulicCategory") as DropDownList;  //数据字典
                ControlHelper.BindDictionaryCategory(dplPulicCategory);
            }
        }


        protected void gridColumns_OnRowDataBound(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {


            }

        }


        protected void dplColumnType_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dplColumnType = (DropDownList)sender;
            GridViewRow row = (GridViewRow)dplColumnType.Parent.Parent;
            var dplPulicCategory = row.FindControl("dplPulicCategory") as DropDownList;
            var panelRelativeTable = row.FindControl("panelRelativeTable") as Panel;
            if (dplColumnType.SelectedValue == ColumnType.ColumnPublicItem.Value)
            {

                dplPulicCategory.Visible = true;
            }
            else
            {
                dplPulicCategory.Visible = false;
            }

            if (dplColumnType.SelectedValue == ColumnType.ColumnRelativeTable.Value)
            {
                panelRelativeTable.Visible = true;
            }
            else
            {
                panelRelativeTable.Visible = false;
            }
        }

        private void BindDictionaryCategory(ListControl objControl)
        {
            var collection = new DictionaryCategoryCollection();
            collection.GetMulti(null);

            objControl.DataSource = collection;
            objControl.DataTextField = "Name";
            objControl.DataValueField = "OID";
            objControl.DataBind();

            objControl.Items.Insert(0, new ListItem
            {
                Text = ColumnType.Empty.Name,
                Value = ColumnType.Empty.Value
            });
        }

 

        private ColumnToActionEntity GetColumnEntityByTableColumnName(string strTableName, string strColumnName)
        {
            var collection = new ColumnToActionCollection();
            collection.GetMulti(ColumnToActionFields.ColumnName == strColumnName &
                                ColumnToActionFields.TableName == strTableName);
            if (collection.Count > 0)
            {
                return collection[0];
            }
            return null;
        }

    }
}
